package com.hdy.redislikerankinglist.utils;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
@Component
@RequiredArgsConstructor
public class RedisUtil<T> {
    private final RedisTemplate redisTemplate;
    private final RedisTemplate<String,String> redisSTemplate;



    /**
     * 当key值不存在时新增缓存返回true，否则返回false
     * @param key
     * @param value
     * @return
     */
    public boolean setNx(String key, T value) {
        return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, value));
    }

    /**
     * 向redis中set集合添加值，点赞
     * @param key
     * @param value
     * @return
     */
    public void sAdd(String key,String value) {
        redisTemplate.opsForZSet().add(key,value,System.currentTimeMillis());
    }

    /**
     * 向redis中set集合移除，取消点赞
     * @param key
     * @param value
     * @return
     */
    public Long sRem(String key,String value) {
        return redisTemplate.opsForZSet().remove(key,value);
    }

    /**
     * 判断key，value是否是集中值
     * @param key
     * @param value
     * @return
     */
    public boolean isMember(String key,String value) {
        //return redisTemplate.opsForZSet().lexCount(key, RedisZSetCommands.Range.range().)
        return redisTemplate.opsForZSet().score(key,value)==null?false:true;
    }

    /**
     * 获取集合大小
     * @param key
     * @return
     */
    public Long size(String key) {
        return redisTemplate.opsForZSet().zCard(key);
    }

    /**
     * 获取集合里所有数据
     * @param key
     * @return
     */
    public Set<String> sMembers(String key) {
        Set<String> members = redisTemplate.opsForZSet().range(key,0,-1);
        return members;
    }

    /*public Long sCard(String aLong) {
        redisTemplate.opsForSet().
        return null;
    }*/
    public void zSScore(String key,String value){
        redisTemplate.opsForZSet().incrementScore(key,value,1);
    }
    public void zSDeScore(String key,String value){
    }
}
